package com.ququ.syb.business.channel.service;

import com.ququ.common.result.Result;
import com.ququ.common.result.ResultJson;
import com.ququ.syb.business.channel.mongo.PayChannelDao;
import com.ququ.syb.business.channel.redis.RedisDao;
import com.ququ.syb.common.constant.PayChannelStatusConstant;
import com.ququ.syb.common.constant.RedisKeyConstant;
import com.ququ.syb.common.pojo.PayChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ququ.syb.business.channel.fegin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PayChannelService {

    private Logger logger = LoggerFactory.getLogger(PayChannelService.class);
    @Autowired
    private ChannelsRouterFegin channelsRouterFegin;
    @Autowired
    private PayChannelDao payChannelDao;
    @Autowired
    private RedisDao redisDao;


    public List<PayChannel> listMyPayChannels(String accountid,String appcode){
        List<PayChannel> myPayChannels = new ArrayList<>();
        Jedis jedis = redisDao.getJedis();
        try{
            Sort sort = new Sort(Sort.Direction.ASC,"paychannelid");
            List<PayChannel> payChannelList = payChannelDao.findAllByStatus(PayChannelStatusConstant.NORMAL,sort);
            Map<String,String> myChannelMap = jedis.hgetAll(RedisKeyConstant.ACCOUNT_PAY_CHANNEL+accountid);
            for (PayChannel payChannel: payChannelList) {
                if (payChannel.getPaychannelid().equals("0")) {
                    myPayChannels.add(payChannel);
                    continue;
                }
                if (myChannelMap.containsKey(payChannel.getPaychannelid())){
                    if (myChannelMap.get(payChannel.getPaychannelid()).equals("1")
                            && jedis.sismember(RedisKeyConstant.APPLICATION_PAY_CHANNEL+appcode,payChannel.getPaychannelid()))
                        myPayChannels.add(payChannel);
                }else {
                    //从通道查询
                    ResultJson result = channelsRouterFegin.isRegister(accountid,payChannel.getPaychannelid());
                    if (result.getStatus() == 1){
                        jedis.hset(RedisKeyConstant.ACCOUNT_PAY_CHANNEL+accountid,payChannel.getPaychannelid(),"1");
                        if (jedis.sismember(RedisKeyConstant.APPLICATION_PAY_CHANNEL+appcode,payChannel.getPaychannelid()))
                            myPayChannels.add(payChannel);
                    }else
                        jedis.hset(RedisKeyConstant.ACCOUNT_PAY_CHANNEL+accountid,payChannel.getPaychannelid(),"0");
                }
            }
            return myPayChannels;
        }finally {
            redisDao.close(jedis);
        }
    }


    public Result<PayChannel> getPayChannel(String paychannelid){
        PayChannel payChannel = payChannelDao.findPayChannelByPaychannelid(paychannelid);
        if (payChannel.getStatus() == PayChannelStatusConstant.NORMAL)
            return new Result<>(payChannel);
        else
            return new Result<>(-1,"通道不存在或通道状态不可用");
    }


    public Result<PayChannel> getPayChannel(String paychannelid,String accountid,String appcode){
        Jedis jedis = redisDao.getJedis();
        try {
            if (!jedis.sismember(RedisKeyConstant.APPLICATION_PAY_CHANNEL+appcode,paychannelid))
                return new Result<>(-1, "本应用不支持这个通道");
            ResultJson result = channelsRouterFegin.isRegister(accountid,paychannelid);
            if (result.getStatus() != 1)
                return new Result<>(-1,"未开通本通道子商户");
            PayChannel payChannel = payChannelDao.findPayChannelByPaychannelid(paychannelid);
            if (payChannel.getStatus() == PayChannelStatusConstant.NORMAL)
                return new Result<>(payChannel);
            else
                return new Result<>(-1, "通道不存在或通道状态不可用");
        }finally {
            redisDao.close(jedis);
        }
    }

}
